package com.zbf.testJSON;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: LI-CI-GAO
 * 作者: licigao
 * 日期: 2021/8/21  11:59
 * 描述:
 */
public class TestJson2 {

    public static void main(String[] args) {

        Map<String,Object> map1=new HashMap<>();

        Map<String,Object> map2=new HashMap<>();
        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        Map<String,Object> map3=new HashMap<>();
        map3.put("skuId","1");
        map3.put("poiId","2");
        map3.put("couponId","566665");
        maps.add(map3);

        map2.put("productList",maps);
        map1.put("name","");
        map1.put("type","");
        map1.put("detail",map2);


        String string = JSON.toJSONString(map1);

       //////////////////////////////////////////// 看下边的代码 上边的不用看


        String json="{\"name\":\"\",\"detail\":{\"productList\":[{\"poiId\":\"2\",\"couponId\":\"566665\",\"skuId\":\"1\"}]},\"type\":\"\"}";

        System.out.println(string);

        JSONObject jsonObject = JSON.parseObject(json);

        JSONObject detail = jsonObject.getJSONObject("detail");

        JSONArray productList = detail.getJSONArray("productList");

        for (Object o : productList) {

            JSONObject jsonObject1 = JSON.parseObject(JSON.toJSONString(o));

            System.out.println("==skuId   =="+jsonObject1.getLong("skuId"));
            System.out.println("==poiId   =="+jsonObject1.getLong("poiId"));
            System.out.println("==couponId=="+jsonObject1.getString("couponId"));

        }
        
        
    }

}
